<?php

/**
 * Implements a menu item
 * @author Pablo
 * @since 1.0
 */
class BlgMenuItem extends BlgData{
	protected $children;
	
	protected function initializeAttributes() {
		parent::initializeAttributes();		
	
		$this->attributes['name']= 		NULL;
		$this->attributes['href']= 		NULL;
		$this->attributes['target']= 	NULL;
		$this->attributes['text']= 		NULL;
		$this->attributes['priority']= 	10;		
		$this->attributes['is_action']= FALSE;
		$this->attributes['selected']= 	FALSE;
		$this->attributes['enabled']= 	TRUE;
		$this->attributes['visible']= 	TRUE;		
		$this->attributes['parent']=	NULL;
		$this->attributes['class']=		NULL;
		
		$this->children= array();
	}
	
	function __construct($vars){
		$this->initializeAttributes();
		
		if(is_array($vars)){
			foreach($vars as $key => $value){
				if(array_key_exists($key, $this->attributes)){
					$this->attributes[$key]= $value;
				}
			}
		}
	}
	
	public function register_child(BlgMenuItem $item){
		if(!array_key_exists($item->name, $this->children) && $item->name!=$this->attributes['name']){
			$item->parent= $this;
			$this->children[$item->name]= $item;			
			return TRUE;
		}
		return FALSE;
	}
	
	public function has_parent(){
		return ($this->attributes['parent'] instanceof BlgMenuItem);
	}
	
	public function get_parent(){
		if($this->has_parent()){
			return $this->attributes['parent'];
		}
		return FALSE;
	}
		
	public function has_children(){
		return (is_array($this->children) && count($this->children)>0);
	}
		
	public function get_children(){
		if($this->has_children()){
			uasort($this->children, 'blg_menu_sort');		
			return $this->children;
		}
		return FALSE;
	}
	
	public function has_brothers(){
		if($this->has_parent()){
			$brothers= $this->get_parent()->get_children();
			if(is_array($brothers) && count($brothers)>1)
				return TRUE;
		}
		return FALSE;		
	}
	
	public function get_brothers(){
		if($this->has_brothers()){
			$brothers= $this->get_parent()->get_children();
			unset($brothers[$this->attributes['name']]);
			if(is_array($brothers) && count($brothers)>0)
				return $brothers;
		}
		return FALSE;
	}
	
	public function mark_as_selected($strict= true){
		if($strict){ // then 'unmark as selected' all other items in tree
			$this->get_root()->unmark_as_selected(true);
		}
		
		$this->attributes['selected']= true;		
		$parent= $this->attributes['parent'];	
		if($parent)
			$parent->mark_as_selected(false);
	}
	
	public function unmark_as_selected($strict= true){
		$this->attributes['selected']= false;
		
		if($strict){ //then 'unmark as selected' all children items (recursive)
			if($this->has_children()){
				foreach($this->get_children() as $child){
					$child->unmark_as_selected($strict);
				}
			}
		}
	}
	
	public function get_root(){
		if($this->has_parent())
			return $this->get_parent()->get_root();
		else
			return $this;
	}
	
	/***********************************************************
	 * 						NOT ABSTRACT
	 ***********************************************************/	
	public function getURL(){ return $this->attributes['href']; }	
	public function save(){ return false; }
	public function delete(){ return false; }	
}